package nl.sodeso.cubicex.command.assertion;

import java.text.SimpleDateFormat;
import java.util.Map;

import nl.sodeso.cubicex.CubicExBaseTestCase;

import org.cubictest.selenium.custom.IElementContext;

import com.thoughtworks.selenium.Selenium;

/**
 * Check if the target match to the specified date format
 * 
 * @author c.beaujoin
 * @since 0.0.4
 */
public class AssertDateFormat extends CubicExBaseTestCase {

	/**
	 * {@inheritDoc}
	 */
	public void executeTest(Map<String, String> arguments, IElementContext context, Selenium selenium) throws Exception {
		// Retrieve the parameters.
		final String _locator = getArgTarget();
		final String _format = getArgFormat();

		// Create the formatted date and properties.
		SimpleDateFormat dateFormat = new SimpleDateFormat(_format);
		dateFormat.setLenient(false);

		// Retrieve a text.
		String _textOfField = selenium.getText(_locator);

		// If Text empty or null
		if (_textOfField == null || _textOfField.equals("")) {
			// Retrieve a field value.
			String _valueOfField = selenium.getValue(_locator);
			try {
				dateFormat.parse(_valueOfField);
				assertEquals("true", "true");
			} catch (Exception e) {
				assertEquals("false", "true");
			}
		} else {
			// Test format
			try {
				dateFormat.parse(_textOfField);
				assertEquals("true", "true");
			} catch (Exception e) {
				assertEquals("false", "true");
			}
		}

	}
}
